package info.itloser.learnkotlindemo.ui.simple

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.core.view.get
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import info.itloser.learnkotlindemo.R
import info.itloser.learnkotlindemo.adapter.FruitRecyclerAdapter
import info.itloser.learnkotlindemo.adapter.FruitRecyclerHorAdapter
import info.itloser.learnkotlindemo.basic.Constants.icons
import info.itloser.learnkotlindemo.bean.Fruit
import info.itloser.learnkotlindemo.databinding.ActivityRecyclerViewBinding

class RecyclerViewActivity : AppCompatActivity(), MenuItem.OnMenuItemClickListener {

    private val fruits = ArrayList<Fruit>()

    private lateinit var binding: ActivityRecyclerViewBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityRecyclerViewBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.tbTop.menu[0].setOnMenuItemClickListener(this)
        binding.tbTop.menu[1].setOnMenuItemClickListener(this)
        binding.tbTop.menu[2].setOnMenuItemClickListener(this)
        binding.tbTop.menu[3].setOnMenuItemClickListener(this)

        //模拟数据
        repeat(8) {
            fruits.add(Fruit(icons[0], "百香果"))
            fruits.add(Fruit(icons[1], "马蹄"))
            fruits.add(Fruit(icons[2], "菠萝"))
            fruits.add(Fruit(icons[3], "草莓"))
            fruits.add(Fruit(icons[4], "车厘子"))
            fruits.add(Fruit(icons[5], "大蒜"))
        }

        //默认布局
        binding.rvMain.layoutManager = LinearLayoutManager(this)
        binding.rvMain.adapter = FruitRecyclerAdapter(fruits)

    }

    /**
     * 菜单的点击事件
     */
    override fun onMenuItemClick(item: MenuItem?): Boolean {
        when (item?.itemId) {
            R.id.menu_empty -> {
                //默认布局
                binding.rvMain.layoutManager = LinearLayoutManager(this)
                binding.rvMain.adapter = FruitRecyclerAdapter(fruits)
            }
            R.id.menu_horizontal -> {
                //水平
                binding.rvMain.layoutManager =
                    LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
                binding.rvMain.adapter = FruitRecyclerHorAdapter(fruits)
            }
            R.id.menu_grid -> {
                //网格

            }
            R.id.menu_staggered -> {
                //瀑布
                binding.rvMain.layoutManager =
                    StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
                binding.rvMain.adapter = FruitRecyclerHorAdapter(fruits)

            }
        }
        return true
    }
}